package com.example.nick.bodyapplication.xml

import android.content.Context
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.InputStream

class PullHelper {
    companion object {
        fun getPersons(xml: InputStream): ArrayList<Person?>? {
            //XmlPullParserFactory pullPaser = XmlPullParserFactory.newInstance();
            //XmlPullParserFactory pullPaser = XmlPullParserFactory.newInstance();
            var persons: ArrayList<Person?>? = null
            var person: Person? = null
            // 创建一个xml解析的工厂
            // 创建一个xml解析的工厂
            val factory = XmlPullParserFactory.newInstance()
            // 获得xml解析类的引用
            // 获得xml解析类的引用
            val parser = factory.newPullParser()
            parser.setInput(xml, "UTF-8")
            // 获得事件的类型
            // 获得事件的类型
            var eventType = parser.eventType
            while (eventType != XmlPullParser.END_DOCUMENT) {
                when (eventType) {
                    XmlPullParser.START_DOCUMENT -> persons = ArrayList()
                    XmlPullParser.START_TAG -> if ("person" == parser.name) {
                        person = Person()
                        // 取出属性值
                        val id = parser.getAttributeValue(0).toInt()
                        person.id = id
                    } else if ("name" == parser.name) {
                        val name = parser.nextText() // 获取该节点的内容
                        person!!.name = name
                    } else if ("age" == parser.name) {
                        val age = parser.nextText().toInt()
                        person!!.age = age
                    }
                    XmlPullParser.END_TAG -> if ("person" == parser.name) {
                        persons!!.add(person)
                        person = null
                    }
                }
                eventType = parser.next()
            }
            return persons
        }
        fun queryXml(ctx: Context, inputStream: InputStream): ArrayList<Person?>? {
            var persons: ArrayList<Person?>? = null
            var person: Person? = null
            val factory = XmlPullParserFactory.newInstance()
            val parser = factory.newPullParser()
            parser.setInput(inputStream, "UTF-8")
            var eventType = parser.eventType
            while (eventType != XmlPullParser.END_DOCUMENT) {
                when (eventType) {
                    XmlPullParser.START_DOCUMENT -> {
                        println("xml pull START_DOCUMENT")
                        persons = ArrayList()
                    }
//                    XmlPullParser.START_TAG -> if ("person" == parser.name) {
//                        person = Person()
//                        // 取出属性值
//                        val id = parser.getAttributeValue(0).toInt()
//                        person.id = id
//                    } else if ("name" == parser.name) {
//                        val name = parser.nextText() // 获取该节点的内容
//                        person!!.name = name
//                    } else if ("age" == parser.name) {
//                        val age = parser.nextText().toInt()
//                        person!!.age = age
//                    }
                    XmlPullParser.START_TAG -> {
                        when(parser.name) {
                            "person" -> {
                                person = Person()
                                person.id = parser.getAttributeValue(0).toInt()
                            }
                            "name" -> {
                                person?.name = parser.nextText()
                            }
                            "age" -> {
                                person?.age = parser.nextText().toInt()
                            }
                        }
                    }
                    XmlPullParser.END_TAG -> {
                        if (parser.name == "person") {
                            persons?.add(person)
                            person = null
                        }
                    }
                }
                eventType = parser.next()
            }
            return persons
        }
    }
}